package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.RoleDao;
import com.bdqn.oa.entity.Role;
import com.bdqn.oa.utils.DruidUtils;
import com.bdqn.oa.utils.Page;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhowei
 * @date 2022/7/20 18:09
 */
public class RoleDaoImpl implements RoleDao {
    private QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增权限
     *
     * @param role
     * @return
     */
    @Override
    public int insertRole(Role role) throws SQLException {
        return queryRunner.update(DruidUtils.getConnection(),"insert into role(`name`,status)value(?,?);",role.getName(),role.getStatus());
    }

    /**
     * 更改权限状态
     *
     * @param role
     * @return
     */
    @Override
    public int updateRoleState(Role role) throws SQLException {
        return queryRunner.update(DruidUtils.getConnection(),"update role set status=? where id=?",role.getStatus(),role.getId());
    }

    /**
     * 查看所有权限信息
     *
     * @return
     */
    @Override
    public List<Role> getRoleByList() throws SQLException {
        List<Role>roleList=new ArrayList<Role>();
        return roleList=queryRunner.query(DruidUtils.getConnection(),"select * from role",new BeanListHandler<Role>(Role.class));
    }

    /**
     * 查看所有权限的分页信息
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Page<Role> getRoleByPage(long pageNumber, long pageSize) throws SQLException {
        Page<Role> rolePage = new Page<Role>();
        rolePage.setPageNumber(pageNumber);
        rolePage.setPageSize(pageSize);
        rolePage.setData(queryRunner.query(DruidUtils.getConnection(),"select id,name,status from role",new BeanListHandler<Role>(Role.class)));
        return rolePage;
    }

    /**
     * 删除权限
     *
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteRole(Role role) throws SQLException {
        return queryRunner.update(DruidUtils.getConnection(),"delete from role where id=?",role.getId());
    }


}
